package com.jakx.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@Component
public class AccountServiceImpl {

    @Autowired
    private ApplicationContext applicationContext;

    private Map<String, Account> factorys = new HashMap<>();

    public void reg(String reg) {
        Account account = null;
        // 从header中解析前端传递的cloudAccountUuid，解析出具体的厂商
        // register account通用业务代码
        // 公共写表代码
//        CloudAccountPojo cloudAccountPojo = new CloudAccountPojo();
//        cloudAccountPojo.setRegionName(request.getRegionName());
//        cloudAccountPojo.setCreateDate(DateUtils.getDateNow());
//        cloudAccountPojo.setDeleted(CloudAccountDeleted.ALIVE);
//        cloudAccountPojo.setStatus(CloudAccountStatus.init);
//        cloudAccountPojo.setSyncPeriod(request.getSyncPeriod());
//        cloudAccountPojo.setSyncStatus(CloudAccountSyncStatus.presync);
//        cloudAccountPojo.setHealthStatus(CloudAccountHealthStatus.exception);
//        cloudAccountPojo.setDescription(request.getDescription());
//        cloudAccountPojo.setName(request.getName());
//        cloudAccountPojo.setRegionId(request.getRegionId());
//        cloudAccountPojo.setProvider(CloudProvider.ALI);
//        cloudAccountPojo.setSecret(request.getSecret());
//        cloudAccountPojo.setUuid(Platform.getUuid());
//        cloudAccountPojo.setProjectId(request.getProjectId());
//        cloudAccountPojo.setTenantId(request.getTenantId());
//        cloudAccountPojo.setUserId(request.getUserId());
//        cloudAccountMapper.saveCloudAccount(cloudAccountPojo);
//        event.setData(cloudAccountPojo);
//        event.setResultCode(ResultCode.SUCCESS);

        // 调用aws创建云主机

        // 状态轮询 1 2 3(厂商调用) 4
        if ("aws".equals(reg)) {
            account = factorys.get("awsAccount");
        } else {
            account = factorys.get("aliAccount");
        }
        account.regAccount();
    }

    @PostConstruct
    public void init() {
        String[] names = applicationContext.getBeanNamesForType(Account.class);
        for (String name : names) {
            Object obj = applicationContext.getBean(name);
            if (obj instanceof Account) {
                factorys.put(name, (Account) obj);
            }
        }

    }
}
